package solutions;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-28
 * Time: 13:04
 */

//https://leetcode.cn/problems/binary-search/submissions/397475039/
class Solution4 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        //int a = new Solution4().search(arr, 6);
        System.out.println(1 >> 1);// 0
        System.out.println((4 - 3) >> 1);// 0
        System.out.println(3 + (4 - 3) >> 1);// 2
        //+ 的优先级比>>高,/的优先级比+高，所以结果不同
        System.out.println(3 + (4 - 3) / 2);// 3

    }

    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            //优先级问题
            //int mid = left + (right - left) >> 1;//false
            //int mid = left + ((right - left) >> 1);//true
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
}
